<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/11/11
 * Time: 17:16
 */

namespace app\admin\model;


class GoldRoomsAgentUser extends DataBase
{
    protected $name = 'gold_rooms_agent_user';

    public function   agentUserData()
    {
        $page_config = array(
            'page' => input('offset', 1),
            'list_rows' => input('limit', 10),
        );
        /*
         * $data = $this
            ->alias('a')
            ->join('fw_users c','a.user_id  =c.id','left')
            ->field('a.*,c.u_id,c.nickname')
            ->paginate($page_config);
         * */

        $data = $this
            ->alias('a')
            ->join('fw_users c','a.user_id  =c.user_id','left')
            ->field('a.*,c.u_number as u_id,c.nickname')
            ->paginate($page_config);

        foreach ($data as $key => $value){
            $data[$key]['nickname'] = preg_replace_callback('/@E(.{6}==)/', function ($r) {
                return base64_decode($r[1]);
            }, $value['nickname']);
        }

        return $data;
    }

    public function agentUserAdd()
    {
        $newData = input('post.');
//        $user_id = User::where('u_id',$newData['u_id'])->value('id');
        $user_id = User::where('u_number',$newData['u_id'])->value('user_id');
        $res = $this->where('user_id',$user_id)->find();
        if($res)  return ['code'=>-100,'msg'=>'该代理已存在'];
        $re = $this->save(['user_id'=>$user_id,'status'=>$newData['status']]);
        dailyLog(['content'=>'添加了代理user_id:'.$user_id,'type'=>3]);
        if($re) return ['code'=>100,'msg'=>'添加成功'];
        else return ['code'=>-100,'msg'=>'添加失败'];
    }
}